﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workbench.Models;
using Workbench.Persistance;
using Workbench.Shared;

namespace Workbench.Operations
{
    public static class POSTaggerOperations
    {

        public static void AddPosTypeToWords(POSTypeModel POSType, IEnumerable<WordModel> words)
        {
            try
            {
                var repo = DBContext.Current.GetPOSTaggerRepository();
                repo.AddPOSTypeForWords(POSType, words);
            }
            catch (Exception ex)
            {
                LoggingService.LogError(ex);
            }
        }

        public static void AddPosTypeToWordInFile(long posTypeId, long uniqueWordId, long fileId)
        {
            try
            {
                var repo = DBContext.Current.GetPOSTaggerRepository();
                repo.AddPOSTypeForWords(posTypeId, uniqueWordId, fileId);
            }
            catch (Exception ex)
            {
                LoggingService.LogError(ex);
            }
        }

        public static void AddPosTypeToWordInAllFiles(long posTypeId, long uniqueWordId)
        {
            try
            {
                var repo = DBContext.Current.GetPOSTaggerRepository();
                repo.AddPOSTypeForWords(posTypeId, uniqueWordId);
            }
            catch (Exception ex)
            {
                LoggingService.LogError(ex);
            }
        }

        public static void RemovePosTypeFromWords(POSTypeModel POSType, IEnumerable<WordModel> words)
        {
            try
            {
                var repo = DBContext.Current.GetPOSTaggerRepository();
                repo.RemovePOSTypeFromWords(POSType, words);
            }
            catch (Exception ex)
            {
                LoggingService.LogError(ex);
                throw new Exception(string.Format("An error occurred while removing POS Type {0} from {1} words", POSType.Name, words.Count()));
            }
        }

        public static void RemovePosTypeFromWordInFile(long posTypeId, long uniqueWordId, long fileId)
        {
            try
            {
                var repo = DBContext.Current.GetPOSTaggerRepository();
                repo.RemovePOSTypeForWords(posTypeId, uniqueWordId, fileId);
            }
            catch (Exception ex)
            {
                LoggingService.LogError(ex);
            }
        }

        public static void RemovePosTypeFromWordInAllFiles(long posTypeId, long uniqueWordId)
        {
            try
            {
                var repo = DBContext.Current.GetPOSTaggerRepository();
                repo.RemovePOSTypeForWords(posTypeId, uniqueWordId);
            }
            catch (Exception ex)
            {
                LoggingService.LogError(ex);
            }
        }

        public static List<POSTypeModel> GetPosTypesForWord(WordModel wordModel)
        {
            using (var buildRepo = DBContext.Current.GetBuildRepository())
            {
                var posTypes = buildRepo.GetPOSTypes().ToDictionary(x => x.RowId);
            }
            var lemmaRepo = DBContext.Current.GetPOSTaggerRepository();
            var assignedPOSTypes = lemmaRepo.GetPOSTypes(wordModel);
            return assignedPOSTypes;
        }
    }
}
